package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.domain.User;
import com.jwinf.cdLibary.service.UserService;
import com.jwinf.cdLibary.tool.WindowUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;


/**
 * @author jwinf
 * 登录窗口控制类
 */
public class LoginController implements Initializable {
    @FXML
    private Label quanxianlbl;
    @FXML
    private Label userlbl;
    @FXML
    private Label pwdlbl;
    @FXML
    private TextField usertxt;
    @FXML
    private PasswordField pwdtxt;
    @FXML
    private TextField pwdhide;
    @FXML
    private Label showpwd;
    @FXML
    private ComboBox<String> quanxian;
    @FXML
    private Button loginbtn;

    private boolean isshow = false;

    // 初始化方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componetInit();               // 组件初始化
        addListener();                // 添加监听事件
    }


    // 组件初始化
    private void componetInit(){

        // 给下拉框设置数据
        ObservableList<String> list = FXCollections.observableArrayList("用户", "管理");
        quanxian.setItems(list);

        // 隐藏可见密码文本框
        pwdhide.setVisible(false);

        // 给标签设置icon样式的图标
        Image image;
        image = new Image(getClass().getResourceAsStream("/icon/userIcon.png"),20,20,false,false);
        userlbl.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/pwdIcon.png"), 20, 20, false, false);
        pwdlbl.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/quanIcon.png"), 16, 16, false, false);
        quanxianlbl.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/showPwdIcon.png"), 16, 16, false, false);
        showpwd.setGraphic(new ImageView(image));

    }

    // 添加监听器
    private void addListener(){
        // 当鼠标进入组件时 改变鼠标样式
        WindowUtils.changeMouse(showpwd);
        WindowUtils.changeMouse(loginbtn);
        WindowUtils.changeMouse(quanxian);
        // 当执行某些耗时操作时改变鼠标样式
        WindowUtils.MouseWait(loginbtn);
        // 当文本框获得焦点时 改变图标大小
        WindowUtils.changeIcon(usertxt, userlbl, "/icon/userIcon.png");
        WindowUtils.changeIcon(pwdhide, pwdlbl, "/icon/pwdIcon.png");
        WindowUtils.changeIcon(pwdtxt, pwdlbl, "/icon/pwdIcon.png");
        WindowUtils.changeIcon(quanxian, quanxianlbl, "/icon/quanIcon.png");
    }

    // 显示密码方法
    @FXML
    private void showPwd(){
        // 判断当且密码显示状况  并通过切换文本框来实现密码的显示和隐藏
        if (!isshow) {
            String password = pwdtxt.getText();
            pwdtxt.setVisible(false);
            pwdhide.setText(password);
            pwdhide.setVisible(true);
            pwdhide.requestFocus();
        } else {
            String password = pwdhide.getText();
            pwdhide.setVisible(false);
            pwdtxt.setText(password);
            pwdtxt.setVisible(true);
            pwdtxt.requestFocus();
        }
        // 切换状态
        isshow = !isshow;
    }


    // 登录方法
    @FXML
    private void login() throws IOException {

        // 获取组件中的数据
        User user = new User();
        String username = usertxt.getText();
        int state = quanxian.getSelectionModel().getSelectedIndex();
        String password;
        if(isshow)
            password = pwdhide.getText();
        else
            password = pwdtxt.getText();

        // 判断数据的合法性并提示用户
        if(username.isEmpty() || password.isEmpty()){
            WindowUtils.showTip("用户名或密码为空", "/icon/warnIcon.png", " -fx-background-color: linear-gradient(to top, #9890e3 0%, #b1f4cf 100%)");
            return;
        }
        if (state == -1){
            WindowUtils.showTip("请选择权限", "/icon/warnIcon.png", " -fx-background-color: linear-gradient(to top, #9890e3 0%, #b1f4cf 100%)");
            return;
        }

        // 给user对象设置数据
        user.setUsername(username);
        user.setPassword(password);
        user.setState(state);
        UserService userService = new UserService();
        // 调用服务层的登录方法
        boolean isSuccess = userService.isLoginSuccess(user);
        // 根据登录结果进行操作 并提示用户
        if(isSuccess){
            if(user.getState() == 0){
                WindowUtils.showFrame("/com/jwinf/cdLibary/view/UserMainFrame.fxml", "/icon/loginIcon.png", "用户主界面", user);
                WindowUtils.destroyStage(loginbtn);
            }else if(user.getState() == 1){
                WindowUtils.showFrame("/com/jwinf/cdLibary/view/AdminMainFrame.fxml", "/icon/loginIcon.png", "管理员主界面");
                WindowUtils.destroyStage(loginbtn);
            }

        }else {
            WindowUtils.showTip("用户名或密码错误", "/icon/warnIcon.png", " -fx-background-color: linear-gradient(to top, #9890e3 0%, #b1f4cf 100%)");
        }
    }

}
